<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="box-logo">
        <!-- logo -->
        <img src="../assets/YuI.png" alt id="logo" />
        <!-- 顶部标题 -->
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 伸缩侧边栏按钮 -->
        <div class="btn-toggle" @click="toggleAsideCollapse">|||</div>
        <!-- 侧边栏菜单 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="skyblue"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="'/' + menu.path" v-for="menu in menuList" :key="menu.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[menu.id]"></i>
              <!-- 文本 -->
              <span>{{menu.authName}}</span>
            </template>
            <!-- 二级子菜单 -->
            <el-menu-item
              :index="'/' + subMenu.path"
              v-for="subMenu in menu.children"
              :key="subMenu.id"
              @click="saveNavState('/' + subMenu.path)"
            >
              <!-- 二级菜单模板 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconsObj[subMenu.id]"></i>
                <!-- 文本 -->
                <span>{{subMenu.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体结构 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: null,
      iconsObj: {
        "125": "iconfont icon-user",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      isCollapse: false,
      activePath: ""
    };
  },
  methods: {
    loginOut() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      }
      this.menuList = res.data;
    },
    toggleAsideCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    saveNavState(path) {
      window.sessionStorage.setItem("activePath", path);
      this.activePath = path;
    }
  },
  created() {
    // 在组件创建完成时请求左侧菜单数据
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  }
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
}

.box-logo {
  display: flex;
  align-items: center;
}

#logo {
  width: 60px;
  height: 60px;
}

.el-aside {
  background-color: #333744;
}

.btn-toggle {
  background-color: #4a5064;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: default;
}

.el-main {
  background: #eaedf1;
}
</style>
